import Vue from 'vue'
import VueRouter from 'vue-router'
import About from '@/components/About'
import Home from '@/components/Home'
import HelloWorld from '@/components/HelloWorld'
import student from '@/components/student/list'
import view from '@/components/student/view'
import edit from '@/components/student/edit'

Vue.use(VueRouter)
const router = new VueRouter({
        routes: [
            {
                path: '/about',
                meta: {isAuth: false, title: '关于'},
                component: About
            },
            {
                path: '/home',
                meta: {isAuth: false, title: '主页'},
                component: Home
            },
            {
                path: '/',
                meta: {isAuth: false},
                component: HelloWorld
            },
            {
                path: '/student',
                component: student,
                meta: {isAuth: true},
                children: [
                    {
                        path: 'view',
                        component: view,
                        props: {},
                        beforeEnter: (to,from,next) => {

                        }
                    },
                    {
                        path: 'edit',
                        name: 'studentEdit',
                        component: edit
                    }
                ]
            },

        ]
    }
)
//全局前置路由守卫
router.beforeEach((to, from, next) => {
    console.log("路由前置守卫", "to:", to.path, "isAuth:", to.meta.isAuth, "from:", from.path)
    //获取登录信息，权限信息
    // if (to.path == '/student') {
    //     alert("没有权限")
    // } else {
    //     next();
    // }
    if (to.meta.isAuth) {
        alert("没有权限")
    } else {
        next();
    }

})

//全局后置路由守卫
router.afterEach((to, from) => {
    console.log("路由后置守卫", "to:", to.path, "isAuth:", to.meta.isAuth, "from:", from.path)
    //可以用来设置title
    if (to.meta.title) {
        document.title = to.meta.title //修改网页的title
    } else {
        document.title = 'vue_test'
    }
})

export default router